<template>
  <div class="agentList divBox">
    <el-card class="box-card" shadow="hover">
      <template class="clearfix" v-slot:header>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="addActivitie"
          >添加公司</el-button
        >
        <!-- <el-tooltip
          :disabled="!disabled"
          class="ml10"
          effect="dark"
          content="本页至少选择一项"
          placement="bottom"
        >
          <div style="display: inline-block">
            <el-button
              :disabled="disabled"
              size="small"
              icon="el-icon-s-release"
              type="primary"
              @click="showActivitieAll(0)"
              >批量隐藏活动</el-button
            >
          </div>
        </el-tooltip> -->
        <!-- <el-tooltip
          :disabled="!disabled"
          class="ml10"
          effect="dark"
          content="本页至少选择一项"
          placement="bottom"
        >
          <div style="display: inline-block">
            <el-button
              :disabled="disabled"
              size="small"
              icon="el-icon-s-claim"
              type="primary"
              @click="showActivitieAll(1)"
              >批量显示活动</el-button
            >
          </div>
        </el-tooltip> -->
        <el-tooltip
          :disabled="!disabled"
          class="ml10"
          effect="dark"
          content="本页至少选择一项"
          placement="bottom"
        >
          <div style="display: inline-block">
            <el-button
              :disabled="disabled"
              size="small"
              icon="el-icon-close"
              type="primary"
              @click="delCompanyAll"
              >批量删除活动</el-button
            >
          </div>
        </el-tooltip>
      </template>
      <el-table
        v-loading="disLoading"
        :data="tableCompanyList"
        style="width: 100%"
        size="mini"
        class="table"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" min-width="55"></el-table-column>
        <el-table-column
          prop="id"
          label="ID"
          align="center"
          min-width="135"
        ></el-table-column>
        <el-table-column
          prop="name"
          label="公司名称"
          align="center"
          min-width="120"
        ></el-table-column>
        <el-table-column
          prop="mobile"
          label="电话"
          align="center"
          min-width="120"
        ></el-table-column>

        <el-table-column
          prop="content"
          label="公司介绍"
          align="center"
          min-width="160"
        > <template slot-scope="{ row }"><div class="content" v-html="row.content"></div></template>
        
      </el-table-column>

        <el-table-column
          prop="sliderImage"
          label="轮播图"
          align="center"
          min-width="160"
        >
          <template slot-scope="{ row }">
            <div class="taoBoxs_img" v-viewer v-if="row.sliderImage">
              <template v-for="image in row.sliderImage.split(',')">
                <img
                  v-lazy="$uploadURL(image.trim())"
                  :key="image"
                  :alt="image"
                />
              </template>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="videos"
          label="视频"
          align="center"
          min-width="160"
        >
          <template slot-scope="{ row }">
            <div class="taoBoxs_img" v-if="row.videos">
              <template v-for="video in row.videos.split(',')">
                <video
                  :src="$uploadURL(video)"
                  class="avatar video-avatar"
                  controls="controls"
                >
                  您的浏览器不支持视频播放
                </video>
              </template>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="isShow"
          label="是否显示公司"
          align="center"
          min-width="120"
        >
          <template slot-scope="{ row }">
            <div>
              {{ row.isShow ? "显示" : "隐藏" }}
            </div>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="230" fixed="right" align="center">
          <template slot-scope="{ row }">
            <el-button type="text" size="small" @click="edit(row.id)"
              >编辑</el-button
            >
            <el-divider direction="vertical"></el-divider>
            <el-button type="text" size="small" @click="delCompany(row.id)"
              >删除</el-button
            >
            <el-divider direction="vertical"></el-divider>
            <el-button type="text" size="small" @click="editMobile(row.id,row.mobile)"
              >编辑手机号</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="spreadBox">
        <pagination
          v-show="pagination.total > 0"
          :total="pagination.total"
          :page.sync="pagination.page"
          :limit.sync="pagination.limit"
          :page-sizes="[10, 20, 30, 50]"
          @pagination="replyChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import {
  getCompanyListApi,
  // activitieDeleteApi,
  deleteCompanyApi,
  patchCompanyMobileApi
} from "@/api/company";
export default {
  name: "Index",
  data() {
    return {
      grid: {
        xl: 5,
        lg: 7,
        md: 12,
        sm: 12,
        xs: 24,
      },
      disForm: {
        keyword: "",
        roleId: "",
      },
      pagination: {
        page: 1,
        limit: 10,
        total: 100,
      },
      tableCompanyList: [],
      del: [],
      disLoading: false,
      disabled: true,
    };
  },
  beforeCreate() {},
  mounted() {
    
    this.getCompanyList();
  },
  computed: {
    ...mapGetters(["customRouter","selectedId"]),
  },
  methods: {
    //添加活动
    addActivitie() {
      this.$router.push({ path: `${this.customRouter}/company/company_add` });
    },
    /**
     * 行选中时
     */
    handleSelectionChange(val) {
      this.$set(
        this,
        "del",
        val.map((item) => item.id)
      );
      console.log(this.del);
      if (val.length > 0) this.$set(this, "disabled", false);
      else this.$set(this, "disabled", true);
    },

    // 编辑手机号码
    editMobile(id){
     this.$prompt('请输入手机号码', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPattern: /^1(3[0-9]|5[0-3,5-9]|7[1-3,5-8]|8[0-9])\d{8}$/,
          inputErrorMessage: '手机号码格式不正确'
        }).then(({ value  }) => {
          console.log("mobile》》》》》",value,id);
          this.patchEditMobile(id,value)
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          });       
        });
    },
    /**
     * 编辑活动
     * id
     */
    edit(id) {
      this.$router.push({
        path: `${this.customRouter}/company/company_add`,
        query: { id: id },
      });
    },
    /**
     * 删除
     */
     delCompany(id) {
      id || (this.del.length !== 0 && !this.disabled)
        ? this.$confirm(`删除公司`, {
            title: `删除公司`,
            message: this.$createElement(
              "div",
              null,
              ["确定要删除公司吗？", "删除公司后将无法恢复，请谨慎操作！"].map(
                (item) => this.$createElement("p", null, item)
              )
            ),
            showCancelButton: true,
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              console.log("删除的id:", id);
              deleteCompanyApi(this.selectedId,!id ? `(${this.del.join()})` : id).then(
                (res) => {
                  if (!id && res) return this.$message.warning(res);
                  this.$message.success("删除公司成功！");
                  this.getCompanyList();
                }
              );
            })
            .catch(() => {
              this.$message({
                type: "info",
                message: `已取消删除活动`,
              });
            })
        : this.$message.warning("请至少选择一条数据");
    },
    // 批量删除
    delCompanyAll() {
      console.log("delectAll", this.del);
      this.delCompany(!this.del.length ? "" : `${this.del.join()}`);
    },

    /**
     * 是否显示活动
     */
     patchEditMobile(id,mobile) {
      console.log(">>>>？",id, mobile);
      let value;
      let ids;
      if (id) {
        value = mobile;
        ids = id;
      } else {
        value = mobile;
        ids = !this.del.length ? "" : `${this.del.join()}`;;
      }
      console.log("value", value);
      console.log("ids", ids);
      patchCompanyMobileApi(
        this.selectedId,[
          {
            op: "replace",
            path: "/mobile",
            value: value,
          },
        ],
        ids
      )
        .then((res) => {
          console.log("修改成功", res);
          this.getCompanyList();
          this.$message.success("修改成功");
        })
        .catch(() => {
          this.$message.error("修改失败");
        });
    },
    /**
     * 获取公司列表
     */
    getCompanyList() {
      console.log(">>>>>>>>>>>>");
      getCompanyListApi(this.selectedId,{
        pageNumber: this.pagination.page,
        pageSize: this.pagination.limit,
        orderby: "Id desc",
        isShow: -1,
        isDel: 0,
      }).then((res) => {
        this.$set(this, "tableCompanyList", res.data);
        this.pagination.total = res.head ? res.head.totalCount : 0;
      });
    },
    replyChange(reply) {
      this.$set(this.pagination, "page", reply.page);
      this.$set(this.pagination, "limit", reply.limit);
      console.log("pagination", this.pagination);
      this.getCompanyList();
    },
  },
};
</script>

<style lang="scss" scoped>
.agentList {
  .taoBox_img {
    width: 36px;
    height: 36px;
    border-radius: 4px;
    overflow: hidden;
    cursor: pointer;

    img {
      width: 100%;
      height: 100%;
    }
  }
  .taoBoxs_img {
    // width: 36px;
    // height: 36px;
    border-radius: 4px;
    // overflow: hidden;
    cursor: pointer;

    img {
      margin-right: 5px;
      width: 36px;
      height: 36px;
    }
    video {
      margin-right: 5px;
      width: 50px;
      height: 50px;
    }
  }

  .el-dropdown-link {
    cursor: pointer;
    color: #409eff;
    font-size: 12px;
  }

  .el-icon-arrow-down {
    font-size: 12px;
  }
}
</style>

<style lang="scss">
.el-dialog.cusDialogCls,
.el-dialog.editAgentDialogCls {
  max-width: 960px;
}

.el-dialog.promotionOrderDialogCls {
  max-width: 900px;
}
</style>